import { createPartMenu } from '../BlockPackage'
import { BeMotor } from '../../../../model/parts/Servo'
import { AppModel } from '../../../../model/AppModel'


export default {

    category: Blockly.Categories.bepart ,
    
    color: [
        Blockly.Colours.bepart.primary,
        Blockly.Colours.bepart.secondary,
        Blockly.Colours.bepart.tertiary
    ] ,

    previousStatement: true ,
    nextStatement: true ,

    init(model:AppModel) {
        this.appendDummyInput()
            .appendField("马达")
            .appendField(createPartMenu(model, BeMotor), 'motor')
            .appendField(new Blockly.FieldDropdown(() => {
                return [
                    ['顺时针' , 'true'] ,
                    ['逆时针' , 'false'] ,
                ]
            }), 'clockwise')
            .appendField("转速%")
        this.appendValueInput("rpm")
    } ,

    run (args: any, util: any, runtime: any, model:AppModel) {
    } ,

    coder: {
        JavaScript(block: any, coder: any, model: AppModel) {

            // 
            let partUUID = block.getFieldValue('motor')
            let part = model.findPartFromUUID(partUUID)
            if(!part)
                return "// unknow Servo part uuid: " + partUUID

            let clockwise = block.getFieldValue('clockwise')
            let rpm = coder.valueToCode(block, 'rpm', coder.ORDER_ASSIGNMENT) || '0'

            return `${coder.indentString()}be.part["${part.instanceName}"].setValue(${rpm}/100,${clockwise})\n`
        }
    }
}
